package com.intelligentmeterreadingsys.util;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

import com.intelligentmeterreadingsys.MainActivity;
import com.intelligentmeterreadingsys.R;

import dbhelper.XmlNameHelper;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class SafetyExitUtil {
    
   private SharedPreferenceUtil sh;
   

   
   private Timer myTimer;
   
   private TimerTask myTimerTask;
   
   public SafetyExitUtil(final Activity activity){
	  
	   sh=new SharedPreferenceUtil(activity);
	   myTimer=new Timer();
	   myTimerTask=new TimerTask() {
		
		@Override
		public void run() {
			
			sh.insertValue(XmlNameHelper.isSafe, false);
			
			Intent intent=new Intent(activity, MainActivity.class);
			activity.startActivity(intent);
			activity.finish();
			
		}
	};
   }
	
   public void start(){
	   String safe=sh.getValue(XmlNameHelper.safetytime);
	   if(myTimer!=null&&!safe.equals("")){
		   
		   Calendar c=Calendar.getInstance();
		
		   c.setTimeInMillis(c.getTimeInMillis()+Integer.parseInt(safe)*60000);
		  
		   myTimer.schedule(myTimerTask, c.getTime());
	   }
   }
   
   public void stop(){
	   if(myTimer!=null){
		  
		   myTimer.cancel();
		   
		   myTimerTask.cancel();
	   }
   }
  
}
